package zbase;

import java.awt.Color;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class MainFrame extends javax.swing.JFrame {

    ArrayList<z_Z> z_Zs = new ArrayList<z_Z>();
    ArrayList<z_Object> z_Objects = new ArrayList<z_Object>();
    ArrayList<z_Service> z_Services = new ArrayList<z_Service>();
    ListSelectionListener lsl = new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            z_Z zz = (z_Z) jTable1.getValueAt(jTable1.getSelectedRow(), 0);
            jLabel2.setText(zz.getK().toString());
            SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm");
            jLabel10.setText(sdf.format(zz.getZdatetime().getTime()));
            if (zz.isZdone()) {
                Calendar tc = (Calendar) zz.getZdatetime().clone();
                tc.add(Calendar.DAY_OF_YEAR, -1);
                if (zz.getZdonedt().before(tc)) {
                    jLabel9.setText("(выполнено)");
                } else {
                    if (zz.getZdonedt().get(Calendar.HOUR_OF_DAY) == 0 && zz.getZdonedt().get(Calendar.MINUTE) == 0) {
                        jLabel9.setText("(выполнено " + new SimpleDateFormat("dd.MM.yyyy").format(zz.getZdonedt().getTime()) + ")");
                    } else {
                        jLabel9.setText("(выполнено " + sdf.format(zz.getZdonedt().getTime()) + ")");
                    }
                }
            } else {
                jLabel9.setText("(не выполнено)");
            }

            jLabel4.setText(findObject(zz.getZobject()));
            jLabel8.setText(findService(zz.getZservice()));

            if (zz.isZalert()) {
                jLabel11.setText("особое внимание");
            } else {
                jLabel11.setText("");
            }

            jTextArea1.setText(zz.getZtext() + "\n\n" + zz.getZprim());
        }
    };

    public MainFrame() {
        initComponents();

        this.setTitle(Vars.getVersion() + " - " + Vars.obj_User.UserName);

        jTable1.setModel(new DefaultTableModel(
                new Object[][]{},
                new String[]{"Номер", "Дата поступления", "Объект", "Текст", "Вып"}) {

            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        });
        jTable1.getColumnModel().getColumn(0).setPreferredWidth(60);
        jTable1.getColumnModel().getColumn(0).setCellRenderer(new MyCellRendererCZ());
        jTable1.getColumnModel().getColumn(1).setPreferredWidth(150);
        jTable1.getColumnModel().getColumn(1).setCellRenderer(new MyCellRendererC());
        jTable1.getColumnModel().getColumn(2).setPreferredWidth(300);
        jTable1.getColumnModel().getColumn(2).setCellRenderer(new MyCellRendererL());
        jTable1.getColumnModel().getColumn(3).setPreferredWidth(500);
        jTable1.getColumnModel().getColumn(3).setCellRenderer(new MyCellRendererL());
        jTable1.getColumnModel().getColumn(4).setPreferredWidth(40);
        jTable1.getColumnModel().getColumn(4).setCellRenderer(new MyCellRendererC());
        jTable1.getSelectionModel().addListSelectionListener(lsl);

        this.setMinimumSize(this.getSize());

        RefreshTable();

        if (jTable1.getRowCount() != 0) {
            jTable1.setRowSelectionInterval(0, 0);
        }

        setAccess();
    }

    private class MyCellRendererC extends DefaultTableCellRenderer {

        @Override
        protected void setValue(Object value) {
            setHorizontalAlignment(SwingConstants.CENTER);
            setText((value == null) ? "" : value.toString());
        }
    }

    private class MyCellRendererCZ extends DefaultTableCellRenderer {

        @Override
        protected void setValue(Object value) {
            setHorizontalAlignment(SwingConstants.CENTER);
            z_Z zz = (z_Z) value;
            if (zz.isZalert()) {
                setFont(getFont().deriveFont(getFont().getStyle() | java.awt.Font.BOLD));
                setForeground(Color.RED);
            } else {
                setFont(getFont().deriveFont(getFont().getStyle() & java.awt.Font.BOLD));
                setForeground(Color.BLACK);
            }
            setText((value == null) ? "" : zz.getK().toString());
        }
    }

    private class MyCellRendererL extends DefaultTableCellRenderer {

        @Override
        protected void setValue(Object value) {
            setHorizontalAlignment(SwingConstants.LEFT);
            setText((value == null) ? "" : value.toString());
        }
    }

    private void RefreshTable() {
        DB db = new DB();
        ResultSet rs;

        WaitingWindow ww = new WaitingWindow();

        RefreshObjects();

        RefreshServices();

        z_Zs.clear();
        String ob1 = "";
        String ob2 = "k";
        if (Vars.prop.getProperty("SortMode").equals("znumr")) {
            ob2 = "k DESC";
        }
        if (Vars.prop.getProperty("SortMode").equals("zdt")) {
            ob2 = "zdatetime";
        }
        if (Vars.prop.getProperty("SortMode").equals("zdtr")) {
            ob2 = "zdatetime DESC";
        }
        if (Vars.prop.getProperty("GroupMode").equals("obj")) {
            ob1 = "zobject, ";
        }
        String sql = "SELECT k, zdatetime, zobject, ztext, zservice, zprim, zalert, zdone, zdonedt FROM zmain WHERE hide = 0 ORDER BY " + ob1 + ob2;
        rs = db.getResultSet(sql);
        try {
            while (rs.next()) {
                Integer k = rs.getInt("k");
                Calendar zdatetime = new GregorianCalendar();
                zdatetime.setTimeInMillis(rs.getTimestamp("zdatetime").getTime());
                Integer zobject = rs.getInt("zobject");
                String ztext = Vars.getStringFromHex(rs.getString("ztext"));
                Integer zservice = rs.getInt("zservice");
                String zprim = Vars.getStringFromHex(rs.getString("zprim"));
                Boolean zalert = rs.getBoolean("zalert");
                Boolean zdone = rs.getBoolean("zdone");
                Calendar zdonedt = new GregorianCalendar(0, 0, 0, 0, 0, 0);
                if (rs.getTimestamp("zdonedt") != null) {
                    zdonedt.setTimeInMillis(rs.getTimestamp("zdonedt").getTime());
                }
                z_Zs.add(new z_Z(k, zdatetime, zobject, ztext, zservice, zprim, zalert, zdone, zdonedt));
            }
        } catch (SQLException ex) {
            Logger.getLogger(ServicesDialog.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NullPointerException ex) {
            Logger.getLogger(ServicesDialog.class.getName()).log(Level.SEVERE, null, ex);
            //Нет доступа к серверу БД или проблемы с таблицами!
        }
        db.closeResultSet();

        Integer selectedk;
        if (jTable1.getSelectedRow() < 0) {
            selectedk = -1;
        } else {
            z_Z selectedzz = (z_Z) jTable1.getModel().getValueAt(jTable1.getSelectedRow(), 0);
            selectedk = selectedzz.getK();
        }
        jTable1.getSelectionModel().removeListSelectionListener(lsl);
        DefaultTableModel tm = (DefaultTableModel) jTable1.getModel();
        while (tm.getRowCount() > 0) {
            tm.removeRow(0);
        }
        int selcounter = 0;
        Integer newseli = -1;
        for (z_Z zz : z_Zs) {
            Object[] rowdata = new Object[5];
            rowdata[0] = zz;
            rowdata[1] = new SimpleDateFormat("dd.MM.yyyy HH:mm").format(zz.getZdatetime().getTime());
            rowdata[2] = findObject(zz.getZobject());
            rowdata[3] = zz.getZtext().replaceAll("\n", " ");
            if (zz.isZdone()) {
                rowdata[4] = "Да";
            } else {
                rowdata[4] = "Нет";
            }
            tm.addRow(rowdata);
            if (zz.getK().equals(selectedk)) {
                newseli = selcounter;
            }
            selcounter++;
        }
        jTable1.getSelectionModel().addListSelectionListener(lsl);

        if (newseli > -1) {
            jTable1.setRowSelectionInterval(newseli, newseli);
        } else {
            jLabel2.setText("");
            jLabel4.setText("");
            jLabel8.setText("");
            jLabel9.setText("");
            jLabel10.setText("");
            jTextArea1.setText("");
        }

        ww.close();
    }

    private void RefreshObjects() {
        DB db = new DB();
        z_Objects.clear();
        ResultSet rs = db.getResultSet("SELECT k, objname FROM objects ORDER BY objname");
        try {
            while (rs.next()) {
                z_Objects.add(new z_Object(rs.getInt("k"), Vars.getStringFromHex(rs.getString("objname"))));
            }
        } catch (SQLException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NullPointerException ex) {
            Logger.getLogger(ObjectsDialog.class.getName()).log(Level.SEVERE, null, ex);
            //Нет доступа к серверу БД или проблемы с таблицами!
        }
        db.closeResultSet();
    }

    private void RefreshServices() {
        DB db = new DB();
        z_Services.clear();
        ResultSet rs = db.getResultSet("SELECT k, servicename FROM services ORDER BY servicename");
        try {
            while (rs.next()) {
                z_Services.add(new z_Service(rs.getInt("k"), Vars.getStringFromHex(rs.getString("servicename"))));
            }
        } catch (SQLException ex) {
            Logger.getLogger(ServicesDialog.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NullPointerException ex) {
            Logger.getLogger(ServicesDialog.class.getName()).log(Level.SEVERE, null, ex);
            //Нет доступа к серверу БД или проблемы с таблицами!
        }
        db.closeResultSet();
    }

    private String findObject(Integer k) {
        for (z_Object zo : z_Objects) {
            if (zo.getK().equals(k)) {
                return zo.getObjname();
            }
        }
        return "";
    }

    private String findService(Integer k) {
        for (z_Service zs : z_Services) {
            if (zs.getK().equals(k)) {
                return zs.getServicename();
            }
        }
        return "";
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        jMenuItem11 = new javax.swing.JMenuItem();
        jSeparator9 = new javax.swing.JSeparator();
        jMenuItem10 = new javax.swing.JMenuItem();
        jToolBar1 = new javax.swing.JToolBar();
        jSeparator7 = new javax.swing.JToolBar.Separator();
        jButton1 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton10 = new javax.swing.JButton();
        jSeparator4 = new javax.swing.JToolBar.Separator();
        jButton3 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jSeparator5 = new javax.swing.JToolBar.Separator();
        jButton4 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        jSeparator6 = new javax.swing.JToolBar.Separator();
        jButton5 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jSeparator8 = new javax.swing.JSeparator();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem6 = new javax.swing.JMenuItem();
        jSeparator10 = new javax.swing.JPopupMenu.Separator();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenu5 = new javax.swing.JMenu();
        jMenuItem16 = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem7 = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JSeparator();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JSeparator();
        jMenuItem15 = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();
        jMenuItem12 = new javax.swing.JMenuItem();
        jMenuItem13 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem14 = new javax.swing.JMenuItem();

        jMenuItem11.setText("Открыть");
        jMenuItem11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem11ActionPerformed(evt);
            }
        });
        jPopupMenu1.add(jMenuItem11);
        jPopupMenu1.add(jSeparator9);

        jMenuItem10.setText("Удалить");
        jMenuItem10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem10ActionPerformed(evt);
            }
        });
        jPopupMenu1.add(jMenuItem10);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("База заявок");

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);
        jToolBar1.add(jSeparator7);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/zbase/resources/new.png"))); // NOI18N
        jButton1.setToolTipText("Новая заявка...");
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton1);

        jButton8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/zbase/resources/pencil.png"))); // NOI18N
        jButton8.setToolTipText("Открыть для редактирования...");
        jButton8.setFocusable(false);
        jButton8.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton8.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton8);

        jButton6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/zbase/resources/remove.png"))); // NOI18N
        jButton6.setToolTipText("Удалить заявку...");
        jButton6.setFocusable(false);
        jButton6.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton6.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton6);

        jButton10.setIcon(new javax.swing.ImageIcon(getClass().getResource("/zbase/resources/viewmag.png"))); // NOI18N
        jButton10.setToolTipText("Найти заявки...");
        jButton10.setFocusable(false);
        jButton10.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton10.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton10ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton10);
        jToolBar1.add(jSeparator4);

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/zbase/resources/icons.png"))); // NOI18N
        jButton3.setToolTipText("Список объектов...");
        jButton3.setFocusable(false);
        jButton3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton3.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton3);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/zbase/resources/iconthemes.png"))); // NOI18N
        jButton2.setToolTipText("Список служб...");
        jButton2.setFocusable(false);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton2);
        jToolBar1.add(jSeparator5);

        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/zbase/resources/tool_clipboard.png"))); // NOI18N
        jButton4.setToolTipText("Подготовка отчета...");
        jButton4.setFocusable(false);
        jButton4.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton4.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton4);

        jButton9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/zbase/resources/log.png"))); // NOI18N
        jButton9.setToolTipText("Статистика...");
        jButton9.setFocusable(false);
        jButton9.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton9.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton9);
        jToolBar1.add(jSeparator6);

        jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/zbase/resources/viewmagfit.png"))); // NOI18N
        jButton5.setToolTipText("Сортировка...");
        jButton5.setFocusable(false);
        jButton5.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton5.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton5);

        jButton7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/zbase/resources/reload.png"))); // NOI18N
        jButton7.setToolTipText("Обновить");
        jButton7.setFocusable(false);
        jButton7.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton7.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton7);

        jSplitPane1.setBorder(null);
        jSplitPane1.setDividerLocation(230);
        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jSplitPane1.setTopComponent(jScrollPane1);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setFont(jLabel1.getFont().deriveFont(jLabel1.getFont().getStyle() & ~java.awt.Font.BOLD));
        jLabel1.setText("Заявка №");

        jLabel2.setFont(jLabel2.getFont().deriveFont(jLabel2.getFont().getStyle() | java.awt.Font.BOLD));
        jLabel2.setText("     ");

        jLabel3.setFont(jLabel3.getFont().deriveFont(jLabel3.getFont().getStyle() & ~java.awt.Font.BOLD));
        jLabel3.setText("Объект:");

        jLabel4.setText("          ");

        jLabel5.setFont(jLabel5.getFont().deriveFont(jLabel5.getFont().getStyle() & ~java.awt.Font.BOLD));
        jLabel5.setText("от");

        jLabel7.setFont(jLabel7.getFont().deriveFont(jLabel7.getFont().getStyle() & ~java.awt.Font.BOLD));
        jLabel7.setText("Служба:");

        jLabel8.setText("          ");

        jLabel9.setText("          ");

        jLabel10.setText("          ");

        jLabel6.setFont(jLabel6.getFont().deriveFont(jLabel6.getFont().getStyle() & ~java.awt.Font.BOLD));
        jLabel6.setText("отметка о выполнении:");

        jLabel11.setFont(jLabel11.getFont().deriveFont(jLabel11.getFont().getStyle() | java.awt.Font.BOLD));
        jLabel11.setForeground(java.awt.Color.red);
        jLabel11.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel11.setText("          ");

        jScrollPane2.setBorder(null);

        jTextArea1.setBackground(javax.swing.UIManager.getDefaults().getColor("Panel.background"));
        jTextArea1.setColumns(20);
        jTextArea1.setEditable(false);
        jTextArea1.setRows(5);
        jTextArea1.setBorder(null);
        jScrollPane2.setViewportView(jTextArea1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 894, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 261, Short.MAX_VALUE)
                        .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel4))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel8))
                    .addComponent(jSeparator8, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 894, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel5)
                    .addComponent(jLabel10)
                    .addComponent(jLabel6)
                    .addComponent(jLabel9)
                    .addComponent(jLabel11))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator8, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE)
                .addContainerGap())
        );

        jSplitPane1.setRightComponent(jPanel1);

        jMenu1.setText("Файл");

        jMenuItem6.setText("Сменить пользователя...");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem6);
        jMenu1.add(jSeparator10);

        jMenuItem4.setText("Выход");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem4);

        jMenuBar1.add(jMenu1);

        jMenu5.setText("Вид");

        jMenuItem16.setText("Сортировка...");
        jMenuItem16.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem16ActionPerformed(evt);
            }
        });
        jMenu5.add(jMenuItem16);

        jMenuItem1.setText("Обновить");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu5.add(jMenuItem1);

        jMenuBar1.add(jMenu5);

        jMenu2.setText("Инструменты");

        jMenuItem7.setText("Новая заявка...");
        jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem7ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem7);
        jMenu2.add(jSeparator3);

        jMenuItem2.setText("Список объектов...");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem2);

        jMenuItem3.setText("Список служб...");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem3);
        jMenu2.add(jSeparator2);

        jMenuItem15.setText("Поиск...");
        jMenuItem15.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem15ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem15);
        jMenu2.add(jSeparator1);

        jMenuItem5.setText("Настройки...");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem5);

        jMenuBar1.add(jMenu2);

        jMenu4.setText("Отчеты");

        jMenuItem12.setText("Подготовка отчета...");
        jMenuItem12.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem12ActionPerformed(evt);
            }
        });
        jMenu4.add(jMenuItem12);

        jMenuItem13.setText("Статистика...");
        jMenuItem13.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem13ActionPerformed(evt);
            }
        });
        jMenu4.add(jMenuItem13);

        jMenuBar1.add(jMenu4);

        jMenu3.setText("Справка");

        jMenuItem14.setText("О программе");
        jMenuItem14.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem14ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem14);

        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 922, Short.MAX_VALUE)
                .addGap(12, 12, 12))
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 946, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 532, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        ObjectsDialog dlg = new ObjectsDialog(this, true);
        dlg.setLocationRelativeTo(null);
        dlg.setVisible(true);
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        ServicesDialog dlg = new ServicesDialog(this, true);
        dlg.setLocationRelativeTo(null);
        dlg.setVisible(true);
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
        dispose();
    }//GEN-LAST:event_jMenuItem4ActionPerformed

    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
        OptionsDialog dlg = new OptionsDialog(this, true);
        dlg.setLocationRelativeTo(null);
        dlg.setVisible(true);
        RefreshTable();
    }//GEN-LAST:event_jMenuItem5ActionPerformed

    private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem7ActionPerformed
        NewZDialog dlg = new NewZDialog(this, true);
        dlg.setLocationRelativeTo(null);
        dlg.setVisible(true);
        RefreshTable();
    }//GEN-LAST:event_jMenuItem7ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        NewZDialog dlg = new NewZDialog(this, true);
        dlg.setLocationRelativeTo(null);
        dlg.setVisible(true);
        RefreshTable();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        ObjectsDialog dlg = new ObjectsDialog(this, true);
        dlg.setLocationRelativeTo(null);
        dlg.setVisible(true);
        RefreshTable();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        ServicesDialog dlg = new ServicesDialog(this, true);
        dlg.setLocationRelativeTo(null);
        dlg.setVisible(true);
        RefreshTable();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        ReportsDialog dlg = new ReportsDialog(this, true);
        dlg.setLocationRelativeTo(null);
        dlg.setVisible(true);
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        FilterDialog dlg = new FilterDialog(this, true);
        dlg.setLocationRelativeTo(null);
        dlg.setVisible(true);
        RefreshTable();
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        if (jTable1.getSelectedRow() == -1) {
            return;
        }
        if (JOptionPane.showConfirmDialog(
                null,
                "Вы уверены, что хотите удалить эту заявку?",
                "Подтверждение",
                JOptionPane.YES_NO_OPTION,
                JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
            int tk = jTable1.getSelectedRow();
            z_Z zz = (z_Z) jTable1.getValueAt(tk, 0);
            DB db = new DB();
            db.executeNonQuery("UPDATE zmain SET hide = 1 WHERE k = " + zz.getK().toString());
            RefreshTable();
            if (tk < jTable1.getRowCount()) {
                jTable1.setRowSelectionInterval(tk, tk);
            } else {
                tk = jTable1.getRowCount();
                tk--;
                if (tk >= 0) {
                    jTable1.setRowSelectionInterval(tk, tk);
                }
            }
        }
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        RefreshTable();
    }//GEN-LAST:event_jButton7ActionPerformed

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
        if (jTable1.getSelectedRow() == -1) {
            return;
        }
        z_Z tzz = (z_Z) jTable1.getValueAt(jTable1.getSelectedRow(), 0);
        EditZDialog dlg = new EditZDialog(this, true, tzz);
        dlg.setLocationRelativeTo(null);
        dlg.setVisible(true);
        RefreshTable();
    }//GEN-LAST:event_jButton8ActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        if (evt.getButton() == java.awt.event.MouseEvent.BUTTON2 || evt.getButton() == java.awt.event.MouseEvent.BUTTON3) {
            int rownumber = jTable1.rowAtPoint(evt.getPoint());
            jTable1.setRowSelectionInterval(rownumber, rownumber);
            jPopupMenu1.show(jTable1, evt.getX(), evt.getY());
        } else {
            if (evt.getClickCount() > 1 && jTable1.getSelectedRow() != -1) {
                z_Z tzz = (z_Z) jTable1.getValueAt(jTable1.getSelectedRow(), 0);
                EditZDialog dlg = new EditZDialog(this, true, tzz);
                dlg.setLocationRelativeTo(null);
                dlg.setVisible(true);
                RefreshTable();
            }
        }
    }//GEN-LAST:event_jTable1MouseClicked

    private void jMenuItem12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem12ActionPerformed
        ReportsDialog dlg = new ReportsDialog(this, true);
        dlg.setLocationRelativeTo(null);
        dlg.setVisible(true);
    }//GEN-LAST:event_jMenuItem12ActionPerformed

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
        StatisticsDialog dlg = new StatisticsDialog(this, true);
        dlg.setLocationRelativeTo(null);
        dlg.setVisible(true);
    }//GEN-LAST:event_jButton9ActionPerformed

    private void jMenuItem14ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem14ActionPerformed
        AboutDialog dlg = new AboutDialog(this, true);
        dlg.setLocationRelativeTo(null);
        dlg.setVisible(true);
    }//GEN-LAST:event_jMenuItem14ActionPerformed

    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed
        FindDialog dlg = new FindDialog(this, true);
        dlg.setLocationRelativeTo(null);
        dlg.setVisible(true);
        if (dlg.getFoundZ() != null) {
            for (int i = 0; i < jTable1.getRowCount(); i++) {
                z_Z tzz = (z_Z) jTable1.getValueAt(i, 0);
                if (tzz.getK().equals(dlg.getFoundZ())) {
                    jTable1.setRowSelectionInterval(i, i);
                }
            }
        }
    }//GEN-LAST:event_jButton10ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        RefreshTable();
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem16ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem16ActionPerformed
        FilterDialog dlg = new FilterDialog(this, true);
        dlg.setLocationRelativeTo(null);
        dlg.setVisible(true);
        RefreshTable();
    }//GEN-LAST:event_jMenuItem16ActionPerformed

    private void jMenuItem13ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem13ActionPerformed
        StatisticsDialog dlg = new StatisticsDialog(this, true);
        dlg.setLocationRelativeTo(null);
        dlg.setVisible(true);
    }//GEN-LAST:event_jMenuItem13ActionPerformed

    private void jMenuItem15ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem15ActionPerformed
        FindDialog dlg = new FindDialog(this, true);
        dlg.setLocationRelativeTo(null);
        dlg.setVisible(true);
        if (dlg.getFoundZ() != null) {
            for (int i = 0; i < jTable1.getRowCount(); i++) {
                z_Z tzz = (z_Z) jTable1.getValueAt(i, 0);
                if (tzz.getK().equals(dlg.getFoundZ())) {
                    jTable1.setRowSelectionInterval(i, i);
                }
            }
        }
    }//GEN-LAST:event_jMenuItem15ActionPerformed

    private void jMenuItem11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem11ActionPerformed
        if (jTable1.getSelectedRow() == -1) {
            return;
        }
        z_Z tzz = (z_Z) jTable1.getValueAt(jTable1.getSelectedRow(), 0);
        EditZDialog dlg = new EditZDialog(this, true, tzz);
        dlg.setLocationRelativeTo(null);
        dlg.setVisible(true);
        RefreshTable();
    }//GEN-LAST:event_jMenuItem11ActionPerformed

    private void jMenuItem10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem10ActionPerformed
        if (jTable1.getSelectedRow() == -1) {
            return;
        }
        if (JOptionPane.showConfirmDialog(
                null,
                "Вы уверены, что хотите удалить эту заявку?",
                "Подтверждение",
                JOptionPane.YES_NO_OPTION,
                JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
            int tk = jTable1.getSelectedRow();
            z_Z zz = (z_Z) jTable1.getValueAt(tk, 0);
            DB db = new DB();
            db.executeNonQuery("UPDATE zmain SET hide = 1 WHERE k = " + zz.getK().toString());
            RefreshTable();
            if (tk < jTable1.getRowCount()) {
                jTable1.setRowSelectionInterval(tk, tk);
            } else {
                tk = jTable1.getRowCount();
                tk--;
                if (tk >= 0) {
                    jTable1.setRowSelectionInterval(tk, tk);
                }
            }
        }
}//GEN-LAST:event_jMenuItem10ActionPerformed

    private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem6ActionPerformed
        LoginDialog ldlg = new LoginDialog(null, true);
        ldlg.setLocationRelativeTo(null);
        ldlg.setVisible(true);
        this.setTitle(Vars.getVersion() + " - " + Vars.obj_User.UserName);
        setAccess();
    }//GEN-LAST:event_jMenuItem6ActionPerformed

    private void setAccess() {
        if (!Vars.obj_User.CanZmain) {
            jMenuItem7.setEnabled(false);
            jMenuItem10.setEnabled(false);
            jButton1.setEnabled(false);
            jButton6.setEnabled(false);
        } else {
            jMenuItem7.setEnabled(true);
            jMenuItem10.setEnabled(true);
            jButton1.setEnabled(true);
            jButton6.setEnabled(true);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenu jMenu5;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem10;
    private javax.swing.JMenuItem jMenuItem11;
    private javax.swing.JMenuItem jMenuItem12;
    private javax.swing.JMenuItem jMenuItem13;
    private javax.swing.JMenuItem jMenuItem14;
    private javax.swing.JMenuItem jMenuItem15;
    private javax.swing.JMenuItem jMenuItem16;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JPopupMenu.Separator jSeparator10;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JToolBar.Separator jSeparator4;
    private javax.swing.JToolBar.Separator jSeparator5;
    private javax.swing.JToolBar.Separator jSeparator6;
    private javax.swing.JToolBar.Separator jSeparator7;
    private javax.swing.JSeparator jSeparator8;
    private javax.swing.JSeparator jSeparator9;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JToolBar jToolBar1;
    // End of variables declaration//GEN-END:variables
}
